package com.plian.system.controller.transfer;

import com.alibaba.fastjson.JSON;
import com.plian.system.dto.fr.procurement.PurchaseTransferFileDTO;
import com.plian.system.entity.fr.procurement.MaterialProcurement;
import com.plian.system.model.R;
import com.plian.system.service.fr.procurement.IPurchaseTransferService;
import com.plian.system.vo.fr.procurement.PurchaseTransferResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @description:
 * @author: gangan
 * @create: 2022-04-21 16:15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("api/purchasetransfer")
@Api(value = "大宗物资采购数据传输接口", tags = "大宗物资采购数据传输接口")
public class PurchaseTransferController {

    private IPurchaseTransferService purchaseTransferService;

    /**
     * 详情
     */
    @PostMapping("/saveList")
    @ApiOperation(value = "大宗物资采购单据", notes = "覆盖原有数据 大宗物资采购单据")
    public R<PurchaseTransferResultVO> saveList(@RequestBody List<MaterialProcurement> materialProcurementList) {
        return R.data(purchaseTransferService.saveList(materialProcurementList));
    }

    @PostMapping(value = "/save/file")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "files", value = "附件", required = true),
            @ApiImplicitParam(name = "fileInfo", value = "附件信息", required = true)
    })
    @ApiOperation(value = "附件", notes = "覆盖原有数据 传入文件、文件信息json fileInfo 列子: [\n" +
            "    {\n" +
            "        \"dataSourceId\":5,\n" +
            "        \"type\":1,\n" +
            "        \"fileMD5List\":[\"c3307d8ca12d84d44e55b298f9d2dc02\"]\n" +
            "    },\n" +
            "     {\n" +
            "        \"dataSourceId\":20,\n" +
            "        \"type\":2,\n" +
            "        \"fileMD5List\":[\"c3307d8ca12d84d44e55b298f9d2dc02\"]\n" +
            "    }\n" +
            "] , dataSourceId 数据源id，type 1决策文件 2合同 fileMD5List 文件md5值")
    public R file(@RequestParam(value = "files") List<MultipartFile> files, @RequestParam String fileInfo){
        List<PurchaseTransferFileDTO> purchaseTransferFileDTOS = JSON.parseArray(fileInfo, PurchaseTransferFileDTO.class);
        return R.status(purchaseTransferService.saveFile(files, purchaseTransferFileDTOS));
    }
}
